package managedbean.Customer;

import entity.CRM.Representative;
import entity.CRM.SaleLead.CustomerType;
import entity.Employee;
import java.util.ArrayList;
import javax.ejb.EJB;
import javax.el.ELContext;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import managedbean.Controller;
import session.stateless.CRM.CustomerBeanLocal;


@ManagedBean(name="addCustomerManagerBean")
@ViewScoped
public class AddCustomerManagerBean {
    @EJB 
    private CustomerBeanLocal customerBeanLocal; 
     
    private String customerName;
    private String registrationNumber;
    private String address;
    private CustomerType customerType;
    private String statusMessage;
 
    public AddCustomerManagerBean() {
    }
    
    public String getCustomerName() { 
        return customerName; 
    } 
    
    public void setCustomerName(String customerName) { 
        this.customerName = customerName; 
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public CustomerType getCustomerType() {
        return customerType;
    }
    public void setCustomerType(CustomerType customerType) {
        this.customerType = customerType;
    }
    
    public String getStatusMessage() { 
        return statusMessage; 
    } 
    public void setStatusMessage(String statusMessage) { 
        this.statusMessage = statusMessage; 
    }

    public String getRegistrationNumber() {
        return registrationNumber;
    }

    public void setRegistrationNumber(String registrationNumber) {
        this.registrationNumber = registrationNumber;
    }

    public void saveNewCustomer(ActionEvent event) 
    { 
        String temp_RegistrationNumber = String.valueOf(registrationNumber);
        String temp_Name = String.valueOf(customerName);
        String temp_address = String.valueOf(address);
        CustomerType temp_CustomerType = customerType;
                
        try 
        {
            ELContext elContext = FacesContext.getCurrentInstance().getELContext();
            Controller controller = (Controller) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "controller");
            Employee executive = controller.getEmployee();
            customerBeanLocal.createNewCustomer(temp_Name, temp_RegistrationNumber, temp_address, temp_CustomerType, new ArrayList<Representative>(), executive.getEmployeeId());
            statusMessage = "New Customer Saved Successfully";  

            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Add New Customer Result: " +statusMessage  +")", "")); 
        }
        catch(Exception ex) 
        { 
            ex.printStackTrace(); 
        } 
    }    
    
         
}
